// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.gameScene = ck.getGameScene()

    },

    onCollisionEnter: function (playerCollider, self) {

        if (this.animing)
            return

        if (this.gameScene.data.state === ck.GameState.Prepare)
            return

        if (this.gameScene.data.state === ck.GameState.TouchDoor)
            return

        if (this.gameScene.data.state === ck.GameState.Finished)
            return

        if (this.gameScene.data.state === ck.GameState.Dead)
            return

        var player = playerCollider.node.getComponent('Player')

        // if (player.canJump())
        //     return

        var playerAabb = playerCollider.world.aabb;
        var playerPreAabb = playerCollider.world.preAabb.clone();

        var selfAabb = self.world.aabb;
        var selfPreAabb = self.world.preAabb.clone();

        if (cc.Intersection.rectRect(selfAabb, playerAabb)) {
            if (playerPreAabb.yMin>=selfAabb.yMax) {
                player.putColliderAboveWorldY(selfAabb.yMax)
                player.startJump()

                this.animing = true
                this.node.getComponent(cc.Animation).play()
                ck.delayCall(this.node, 0.5, this.onAnimFinished.bind(this))
            }
        }
    },

    onAnimFinished(){
        this.animing = false
    },
});
